/*-------------------------------------------------------------------------
 * CLR版本:     4.0.30319.42000
 * 创建人:      song
 * 创建时间：   2024/4/9 17:56:20
 * 功 能：      Shader Component Base 绘制模块组件基类
 *-------------------------------------------------------------------------*/

#if UNITY_EDITOR
namespace SCore.Editor.SShaderDraw
{
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;

    public abstract class SComponentGUIBase : ShaderGUI
    {
        protected delegate void resetFunc();

        public List<SPropBase> lProps = new List<SPropBase>();
        public List<string> drawedPropertyNames = new List<string>();
        private GUIStyle btnSytle = new GUIStyle();
        protected Dictionary<string, MaterialProperty> properties = new Dictionary<string, MaterialProperty>();
        public abstract void RefreshProperties();

        protected MaterialEditor materialEditor;

        protected Color ResetPartColor = new Color(1f, 1f, 0.9f);
        public abstract void DrawProperties();

        public abstract void Changed();

        public SComponentGUIBase(MaterialEditor materialEditor, Dictionary<string, MaterialProperty> dics)
        {
            this.materialEditor = materialEditor;
            this.properties = dics;
            this.lProps.Clear();
        }

        protected void DrawPartTitle<T>(SPropSuperBase<T> toggleProp, GUIContent lable, float space,
            params SPropBase[] ps)
        {
            GUILayout.BeginHorizontal();
            if (toggleProp.Exist)
            {
                toggleProp.Draw(0f, 40f);
            }
            else
            {
                GUILayout.Label(lable);
            }

            GUI.color = this.ResetPartColor;
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(100f) };
            if (GUILayout.Button("重置本模块", options))
            {
                foreach (SPropBase base2 in this.lProps)
                {
                    if (base2 == toggleProp)
                    {
                        continue;
                    }

                    base2.Reset();
                }
            }

            GUI.color = Color.white;
            GUILayout.EndHorizontal();
            if (toggleProp.BoolValue)
            {
                foreach (var prop in ps)
                {
                    prop.Draw(space, 40);
                }
            }
        }

        protected void DrawPartTitle(GUIContent lable, ref bool foldout, resetFunc func, float space)
        {
            GUILayout.BeginHorizontal();
            if (foldout)
            {
                btnSytle = SShaderEditorHelpr.GetInternelStyle("BoldTextField");
            }
            else
            {
                btnSytle = SShaderEditorHelpr.GetInternelStyle("BoldTextField");
            }

            GUILayout.Space(space);
            if (GUILayout.Button(lable, btnSytle))
            {
                foldout = !foldout;
            }

            GUI.color = this.ResetPartColor;
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(100f) };
            if (GUILayout.Button("重置本模块", options))
            {
                if (func != null)
                {
                    func();
                }
            }

            GUI.color = Color.white;
            GUILayout.EndHorizontal();
        }
    }
}
#endif